﻿#include "logtabpanel.h"

#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHeaderView>

LogTabPanel::LogTabPanel(QWidget *parent, const LogQueue::LogType type)
    : QWidget(parent)
    , c_type(type)
{
    // 创建选项卡内容,添加选项卡
    this->setObjectName("softRunTab");

    m_logQueue = new LogQueue(this);

    m_startDateTime.setDate(QDate::currentDate().addDays(1 - LOAD_LOG_DEFAULT));
    m_startDateTime.setTime(QTime(0, 0, 0));
    m_dtStart = new QDateTimeEdit(m_startDateTime, this);
    m_dtStart->setObjectName("dtStart");
    m_dtStart->setCalendarPopup(true);
    m_dtStart->setDisplayFormat("yyyy-MM-dd HH");

    m_stopDateTime = QDateTime::currentDateTime();
    m_dtStop = new QDateTimeEdit(m_stopDateTime, this);
    m_dtStop->setObjectName("dtStop");
    m_dtStop->setCalendarPopup(true);
    m_dtStop->setDisplayFormat("yyyy-MM-dd HH");

    connect(m_dtStart, &QDateTimeEdit::dateTimeChanged, this,  &LogTabPanel::onDateTimeChanged);
    connect(m_dtStop, &QDateTimeEdit::dateTimeChanged, this,  &LogTabPanel::onDateTimeChanged);

    m_chkCollect  = new QCheckBox("汇总", this);
    m_chkCollect->setObjectName("chkCollect");
    connect(m_chkCollect, &QCheckBox::clicked, this, &LogTabPanel::onFilterStr);

    m_searchEntry = new SearchEntry(this);
    m_searchEntry->setObjectName("searchEntry");
    //m_searchEntry->setMaximumWidth(500);

    auto hlayLogFilter = new QHBoxLayout();
    hlayLogFilter->setObjectName("hlayLogFilter");
    hlayLogFilter->setContentsMargins(0, 0, 0, 0);

    hlayLogFilter->addWidget(new QLabel("开始时间",this));
    hlayLogFilter->addWidget(m_dtStart);
    hlayLogFilter->addStretch(1);

    hlayLogFilter->addWidget(new QLabel("截至时间",this));
    hlayLogFilter->addWidget(m_dtStop);
    hlayLogFilter->addStretch(3);

    hlayLogFilter->addWidget(m_chkCollect);
    hlayLogFilter->addStretch(3);

    hlayLogFilter->addWidget(m_searchEntry);
    hlayLogFilter->addStretch(21);

    connect(m_searchEntry, &SearchEntry::sendSearchStr, this, &LogTabPanel::onFilterStr);
    connect(m_searchEntry, &SearchEntry::editingFinished, this, &LogTabPanel::onTextIsEmpty);

    // 设置加载log表
    m_tbwLogTable = new QTableWidget(this);
    m_tbwLogTable->setObjectName("tbwSoftRunLog");
    m_tbwLogTable->setFrameShape(QFrame::NoFrame);

    m_tbwLogTable->setColumnCount(3);
    QStringList headers;
    headers << "日期时间" << "类型" << "信息";
    m_tbwLogTable->setHorizontalHeaderLabels(headers);

    m_tbwLogTable->setColumnWidth(0, 200);
    m_tbwLogTable->setColumnWidth(1, 150);
    QHeaderView *horizontalHeader = m_tbwLogTable->horizontalHeader();
    horizontalHeader->setSectionResizeMode(2, QHeaderView::Stretch);

    // 加载默认数据
    onDateTimeChanged();

    auto vlayMain = new QVBoxLayout(this);
    vlayMain->setObjectName("vlaySoftRunTab");
    this->setLayout(vlayMain);

    vlayMain->addLayout(hlayLogFilter);
    vlayMain->addWidget(m_tbwLogTable);
}

void LogTabPanel::onDateTimeChanged()
{
    QDateTime start = m_dtStart->dateTime();
    QDateTime stop = m_dtStop->dateTime();
    if (start == m_startDateTime && stop == m_stopDateTime && m_tbwLogTable->rowCount() != 0) {
        return;
    }

    m_tbwLogTable->clearContents();

    m_logQueue->loadLog(start, stop, c_type);
    int logQueueLen = (int)m_logQueue->size();

    m_tbwLogTable->setRowCount(logQueueLen);
    for (int i = logQueueLen - 1; i >= 0; i--) {
        auto tmp = QDateTime(std::get<0>(m_logQueue->front()) ,
                             std::get<1>(m_logQueue->front())).toString("yyyy-MM-dd HH:mm:ss");
        m_tbwLogTable->setItem(i, 0, new QTableWidgetItem(tmp));
        m_tbwLogTable->setItem(i, 1, new QTableWidgetItem(std::get<2>(m_logQueue->front())));
        m_tbwLogTable->setItem(i, 2, new QTableWidgetItem(std::get<3>(m_logQueue->front())));
        m_logQueue->popLog();
    }

    m_chkCollect->setChecked(false);
    m_searchEntry->clear();

    m_startDateTime = m_dtStart->dateTime();
    m_stopDateTime = m_dtStop->dateTime();
}

void LogTabPanel::onFilterStr()
{
    QStringList filters;
    if (m_chkCollect->isChecked()) {
        filters.append("本次");
    }
    if (!m_searchEntry->text().trimmed().isEmpty()) {
        filters.append(m_searchEntry->text().trimmed());
    }

    if (filters.isEmpty()) {
        showAllTabWidget();
    } else {
        logTextSearch(filters);
    }
}

void LogTabPanel::onTextIsEmpty()
{
    if (m_searchEntry->text().trimmed().isEmpty()) {
        showAllTabWidget();
    }
}

void LogTabPanel::showAllTabWidget()
{
    for (int i = 0; i < m_tbwLogTable->rowCount(); i++) {
        m_tbwLogTable->setRowHidden(i, false);
    }
}

void LogTabPanel::logTextSearch(const QStringList &strList)
{
    int logLen = m_tbwLogTable->rowCount();
    for (int i = 0; i < logLen; i++) {
        m_tbwLogTable->setRowHidden(i, false);
        for (auto item : strList) {
            if (!m_tbwLogTable->item(i, 2)->text().contains(item)) {
                m_tbwLogTable->setRowHidden(i, true);
            }
        }
    }
}
